package org.smoozik.app.mode;

import java.util.ArrayList;
import java.util.List;

import org.smoozik.app.common.partition.component.UIPartition;
import org.smoozik.app.ihm.Constants;
import org.smoozik.services.musictheory.Chord;
import org.smoozik.services.musictheory.Figure;
import org.smoozik.services.musictheory.Interval;
import org.smoozik.services.musictheory.Note;
import org.smoozik.services.musictheory.NoteGenerator;

public class FigureManager {

	// private List<UINote> noteList;
	// private List<UIChord> chordList;
	private List<Figure> figureList;
	private UIPartition partition;

	private Figure currentFigure = null;
//	private Sequence currentSequence = null;

	public FigureManager(UIPartition partition) {
		super();
		this.partition = partition;

	}

	// Getters and setters

	public Figure getCurrentFigure() {
		return currentFigure;
	}

//	public Sequence getCurrentSequence() {
//		return currentSequence;
//	}
//
//	public void setCurrentSequence(Sequence currentSequence) {
//		this.currentSequence = currentSequence;
//	}

	public void setCurrentFigure(Figure currentFigure) {
		this.currentFigure = currentFigure;
	}

	public UIPartition getPartition() {
		return partition;
	}

	public void setPartition(UIPartition partition) {
		this.partition = partition;
	}

	//
	// public List<UINote> getNoteList() {
	// return noteList;
	// }
	//
	// public void setNoteList(List<UINote> noteList) {
	// this.noteList = noteList;
	// }
	//
	// public List<UIChord> getChordList() {
	// return chordList;
	// }
	//
	// public void setChordList(List<UIChord> chordList) {
	// this.chordList = chordList;
	// }
	//
	// // Methods
	//
	// public void resetNoteList() {
	// this.noteList = new ArrayList<>();
	// }
	//
	// public void resetChordList() {
	// this.chordList = new ArrayList<>();
	// }

	public List<Figure> getFigureList() {
		return figureList;
	}

	public void setFigureList(List<Figure> figureList) {
		this.figureList = figureList;
	}

	public void resetFigureList() {
		this.figureList = new ArrayList<>();
	}

	public Figure getRandomNote(int gereDieseBemol) {
		Note note = NoteGenerator.getRandomNote(this.partition, gereDieseBemol);

		return note;
	}

	public Figure getRandomChord(int gereDieseBemol, int renversement) {
		// generates the fundamental of the chord
		Note note = NoteGenerator.getRandomNote(this.partition, gereDieseBemol);
		Chord chord = new Chord(note);

		// generates the type of the chord
		int alt = NoteGenerator.getRandomFromBounds(0,
				Constants.CHORDS.length - 1);
		chord.setIntervals(Constants.CHORDS[alt]);

		// revert the chord
		if (renversement == 1) {
			int nb = NoteGenerator.getRandomFromBounds(0,
					chord.getNbNotes() - 1);
			chord.renversement(nb);
			// System.out.println(chord + " renv " + nb);
		}
		System.out.println(chord.toString2());

		return chord;
	}

//	public static void main(String[] args) {
//		UIPartition partition = new UIPartition(Constants.DEFAULT_KEY);
//		FigureManager fm = new FigureManager(partition);
//
//		Chord f = null;
//
//		for (int i = 0; i < 20; i++) {
//			f = (Chord) fm.getRandomInterval(1);
//			System.out.println(f.toString() + " - " + f.toString2());
//		}
//	}

	public Figure getRandomInterval(int gereDieseBemol) {
		Note note = NoteGenerator.getRandomNote(this.partition, gereDieseBemol);
		Chord chord = new Chord(note);
		Interval interval = NoteGenerator.getRandomIntervalle();
		chord.setInterval(interval);

		return chord;
	}

	public Figure getInterval(String noteLabel, String intervalLabel) {
		Chord chord = null;
		// TODO
		return chord;
	}

	public Figure getNote(String noteLabel) {
		Note note = null;
		// TODO
		return note;
	}

	public Figure getChord(String noteLabel, String name) {
		Chord chord = null;
		// TODO
		return chord;
	}

	public int getKey() {
		int key = 0;

		if (this.partition != null) {
			key = this.partition.getKey();
		}

		return key;
	}

	public void setKey(int key) {
		if (this.partition != null) {
			this.partition.setKey(key);
		}
	}

	public int getNbNotes() {
		int nbNotes = 0;

		if (this.partition != null) {
			nbNotes = this.partition.getNbNotes();
		}

		return nbNotes;
	}

	public void setNbNotes(int nbNotes) {
		if (this.partition != null) {
			this.partition.setNbNotes(nbNotes);
		}
	}

	/**
	 * 
	 * Generates a new figure according to the given mode.
	 * 
	 * The new figure is different than the current figure
	 * 
	 * @param mode
	 *            NOTE_LABEL, CHORD_LABEL or INTERVAL_LABEL
	 * @param paramSharpBemol
	 *            0 generates no sharp or bemol. 1 may generate with sharp or
	 *            bemol
	 * 
	 * @return
	 */
	public Figure addNewFigure(String mode, int paramSharpBemol,
			int paramRenversement) {
		String oldFigure = null;
		String newFigure = null;
		Figure figure = null;

		if (this.currentFigure != null) {
			oldFigure = this.currentFigure.toString();
		} else {
			oldFigure = "null oldFigure";
		}

		// generate a new note different than the old one
		do {
			if (Constants.NOTE_LABEL.equals(mode)) {
				figure = getRandomNote(paramSharpBemol);
			} else if (Constants.CHORD_LABEL.equals(mode)) {
				figure = getRandomChord(paramSharpBemol, paramRenversement);
			} else if (Constants.INTERVAL_LABEL.equals(mode)) {
				figure = getRandomInterval(paramSharpBemol);
			} else {
				figure = null;
			}

			if (figure != null) {
				newFigure = figure.toString();
			} else {
				newFigure = "null newFigure";
			}

		} while (oldFigure.equals(newFigure));

		if (figure != null) {
			this.currentFigure = figure;
		}

		return figure;
	}

	/**
	 * 
	 * Generates a new figure according to the given mode.
	 * 
	 * The new figure is different than the current figure
	 * 
	 * @param mode
	 *            NOTE_LABEL, CHORD_LABEL or INTERVAL_LABEL
	 * @param paramSharpBemol
	 *            0 generates no sharp or bemol. 1 may generate with sharp or
	 *            bemol
	 * 
	 * @return
	 */
	public Figure addLabelFigure(String mode, String label,
			String extent) {
		Figure figure = null;

		// generate a new note different than the old one
		if (Constants.NOTE_LABEL.equals(mode)) {
			figure = getNote(label);
		} else if (Constants.CHORD_LABEL.equals(mode)) {
			figure = getChord(label, extent);
		} else if (Constants.INTERVAL_LABEL.equals(mode)) {
			figure = getInterval(label, extent);
		} else {
			figure = null;
		}

		if (figure != null) {
			this.currentFigure = figure;
		}

		return figure;
	}

//	public void moveAllPointBy(int speed) {
//		int margin;
//
//		// // notes printing
//		// if (noteList != null) {
//		// for (UINote note : noteList) {
//		// if (note != null) {
//		// margin = note.getMargin();
//		// note.setMargin(margin + speed);
//		// }
//		// }
//		// }
//		// // notes printing
//		// if (chordList != null) {
//		// for (UIChord chord : chordList) {
//		// if (chord != null) {
//		// margin = chord.getMargin();
//		// chord.setMargin(margin + speed);
//		// }
//		// }
//		// }
//		// figures moving
//		if (figureList != null) {
//			for (Figure figure : figureList) {
//				if (figure != null) {
//					margin = figure.getMargin();
//					figure.setMargin(margin + speed);
//				}
//			}
//		}
//	}

//	public void refreshNotes() {
//		// // notes printing
//		// if (noteList != null) {
//		// for (UINote note : noteList) {
//		// if (note != null) {
//		// note.prepareDisplay();
//		// }
//		// }
//		// }
//		// // notes printing
//		// if (chordList != null) {
//		// for (UIChord chord : chordList) {
//		// if (chord != null) {
//		// chord.prepareDisplay();
//		// }
//		// }
//		// }
//		// notes refreshing
//		if (figureList != null) {
//			for (Figure figure : figureList) {
//				if (figure != null) {
//					figure.prepareDisplay();
//				}
//			}
//		}
//	}

}
